package cn.maxwell.basic.service.impl;

import cn.maxwell.basic.mapper.BaseMapper;
import cn.maxwell.basic.query.BaseQuery;
import cn.maxwell.basic.service.IBaseService;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.order.query.OrderPetAcquisitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * service接口实现类的公共类
 * @param <T>
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    //注入mapper接口
    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Transactional
    @Override
    public List<T> loadAll() {

        return baseMapper.loadAll();
    }

    /**
     * 添加
     *
     * @param t
     */
    @Transactional
    @Override
    public void add(T t) {
       baseMapper.add(t);
    }

    /**
     * 修改
     *
     * @param t
     */
    @Transactional
    @Override
    public void update(T t) {
       baseMapper.update(t);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
       baseMapper.delete(id);
    }

    /**
     * 分页查询
     *
     * @param baseQuery
     * @return
     */
    @Transactional
    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //查询总条数
        Integer count =baseMapper.queryCount(baseQuery);
        if (count < 1) {
            return new PageList<T>();
        }
        //根据limit分页查询 返回一个第几页第几页的集合
        List<T> list =baseMapper.queryData(baseQuery);

        return new PageList<T>(count, list);  //count==totals /list==data(要展示的数据)
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
       baseMapper.patchDelete(ids);
    }
}
